<template>
    <div id="product-home">
      <nav class="top-navbar" :class="{ fixed: isSticky }">
        <div class="back">
          <button>&lt;</button>
        </div>
        <div class="nav-right">
          <a href="#" class="nav-link">首页</a>
          <a href="#" class="nav-link">个人中心</a>
          <a href="#" class="nav-link">购物车</a>
          <a href="#" class="nav-link">收藏夹</a>
          <a href="#" class="nav-link">帮助中心</a>
          <button class="sign-in-button">Sign in</button>
          <button class="register-button">Register</button>
        </div>
      </nav>
      <div class="product-list-container">
        <h2>商品列表</h2>
        <div class="product-list">
          <div
            class="product-item"
            v-for="(product, index) in products"
            :key="index"
            @click="goToDetail(product.id)"
          >
            <img :src="product.image" alt="product" class="product-image" />
            <div class="product-info">
              <span class="product-name">{{ product.name }}</span>
              <span class="product-price">¥{{ product.price }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, onMounted, onBeforeUnmount } from "vue";
  import { useRouter } from "vue-router";
  
  const products = ref([
    { id: 1, name: "商品1", price: 100, image: "https://via.placeholder.com/150" },
    { id: 2, name: "商品2", price: 200, image: "https://via.placeholder.com/150" },
    { id: 3, name: "商品3", price: 150, image: "https://via.placeholder.com/150" },
    { id: 4, name: "商品4", price: 300, image: "https://via.placeholder.com/150" },
  ]);
  
  const isSticky = ref(false);
  const router = useRouter();
  
  const handleScroll = () => {
    isSticky.value = window.pageYOffset > 0;
  };
  
  const goToDetail = (productId) => {
    router.push({ name: "ProductDetail", params: { id: productId } });
  };
  
  onMounted(() => {
    window.addEventListener("scroll", handleScroll);
  });
  
  onBeforeUnmount(() => {
    window.removeEventListener("scroll", handleScroll);
  });
  </script>
  
  <style scoped>
  * {
  margin: 0 auto;
  padding: 0;
}
#app {
  width: 100vw;
  height: auto;
  margin: 0;
  padding-top: 0;
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
  .product-list-container {
    padding: 20px;
  }
  .product-list {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
  }
  .product-item {
    width: 200px;
    border: 1px solid #ccc;
    padding: 10px;
    cursor: pointer;
    text-align: center;
  }
  .product-image {
    width: 100%;
    height: auto;
  }
  .product-info {
    margin-top: 10px;
  }
  .product-name {
    font-size: 16px;
    font-weight: bold;
  }
  .product-price {
    color: #333;
  }
.top-navbar {
  display: flex;
  justify-content: space-between;
  gap: 20vh;
  background-color: #f4f4f4;
  padding: 10px;
  position: relative;
  z-index: 100px;
  transition: all 0.3s ease;
}
.back {
  display: flex;
  justify-content: start;
}
.nav-right {
  display: flex;
  align-content: flex-end;
}
.nav-link:hover {
  color: white;
  background-color: #333;
  border-radius: 20%;
}
.fixed {
  position: sticky;
  top: 0;
  width: 100%;
  background-color: #f4f4f4;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.nav-link {
  margin-right: 20px;
  text-decoration: none;
  color: #333;
}
.sign-in-button,
.register-button,
.sign-out-button {
  background-color: #333;
  color: white;
  border: none;
  padding: 5px 10px;
  cursor: pointer;
}
  </style>